import pandas as pd
import math
import numpy as np


'''在小偏心计算中，获取符合条件的相对受压区计算高度，同时返回远端钢筋的压(拉)力'''
def cal_bs(roots,eb,fy):
    for i in roots:
        if i>eb and i<=1:
            x = i
    if x <= 1.6-eb:
        return [fy*((0.8-x)/(0.8-eb)),x]
    else:
        return [-fy,x]


'''大偏心求解远端钢筋面积As,和近端钢筋的面积As1'''
def big_hr(fy,fc,N,b,h,a,a1,e,eo,eb,asb,rb_cat,n):
    As1 = (1.2*N*e-asb*fc*b*(h-a)**2)/(fy*(h-a-a1)) #根据节省钢筋的原理，使用界限截面抵抗矩系数
    print('使用界限截面抵抗矩系数,求得的近端钢筋的面积As1为:%.3f'%As1)
    p_min = p_min_data[rb_cat] #获取最小配筋率
    p_min_ac = As1/(b*(h-a))
    e2 =1 - math.sqrt(1-2*asb)

    '''通过判断远端钢筋是否满足最小配筋率'''
    print('配筋率----As1/(b*(h-a))=%.3f   最小配筋率----%.3f'%(p_min_ac,p_min))
    if As1<0 or (p_min_ac < p_min):
        '''不满足，以最小配筋率来计算远端面积'''
        print('近端钢筋不满足最小配筋率，以最小配筋率来计算近端钢筋的面积')
        As1 = p_min*b*(h-a)
        print('使用最小配筋率计算,得近端钢筋的面积As1为:%.3f'%As1)
        as0 = (1.2*N*e-fy*As1*(h-a-a1))/(fc*b*(h-a)**2)
        print('根据所求的As1,计算截面地抗拒系数为:%.3f'%as0)
        e2 =1 - math.sqrt(1-2*as0)
        x = e2*(h-a)
        print('计算得受压区计算高度----x:%.3f   2a1:%.3f'%(x,2*a1))
        if x >=2*a1:
            print('x>=2a1,近端钢筋可以达到轴受压设计值。')
            As = (fc*b*e2*(h-a)+fy*As1-1.2*N)/fy
        else:
            print('x<2a1,以近端钢筋来取矩.')
            e1 = n*eo-h/2+a1
            print('压力N到近端钢筋的距离----e1为:%.3f'%e1)
            As = 1.2*N*e1/(fy*(h-a-a1))
    else:
        print('近端钢筋满足最小配筋率')
        As = (fc*b*eb*(h-a)+fy*As1-1.2*N)/fy
    print('最终---As:',As)
    print('最终---As1:',As1)
    print('相对受压区计算高度:%.3f'%e2)
    return [e2,As,As1]


'''小偏心求解远端钢筋面积As,和近端钢筋的面积As1'''
def small_hr(rb_cat,b,h,a,a1,eb,e,fc):
    p_min = p_min_data[rb_cat]
    ho = h-a #计算有效高度
    As = p_min*b*ho
    """ 以下为求解二次等式 """
    t_1 = -0.5*fc*b*ho*ho
    t_2 = fc*b*ho*ho-((ho-a1)*fy*As)/(0.8-eb)-(ho-a1)*fc*b*ho
    t_3 = (ho-a1)*fy*As*(0.8/(0.8-eb))-1.2*N*e+1.2*N*(ho-a1)
    coefficients = [t_1,t_2,t_3]
    roots = np.roots(coefficients)
    fy_new = cal_bs(roots,eb,fy)[0] #远端钢筋的压(拉)力
    print('计算得远端钢筋的拉应力为：%.3f'%fy_new)
    e_new = cal_bs(roots,eb,fy)[1] #获得相对受压区的计算高度
    print('计算得相对受压区计算高度为：%.3f'%e_new)
    As1 = (1.2*N+fy_new*As-fc*b*ho*e_new)/fy
    p1_min = As1/(b*ho) 

    '''判断近端钢筋是否满足最小配筋率'''
    if p1_min < p_min:
        print('近段As1的钢筋不满足最小配筋率,按最小配筋率计算As1面积')
        As1 = p_min*b*ho

    else:
        print('近段As1的钢筋满足最小配筋率')
    print('As:%.3f'%As,'As1:%.3f'%As1)
    return [e_new,As,As1]


rb_data = {'HPB300': 270, 'HRB335': 300, 'HRB400': 360, 'RRB400': 360}
fc_data = {'C10':4.8, 'C15': 7.2, 'C20': 9.6, 'C25': 11.9, 'C30': 14.3, 'C35': 16.7, 'C40': 19.1, 'C45': 21.1, 'C50': 23.1, 'C55': 25.3, 'C60': 27.5}
asb_data = {'HPB300':0.410, 'HRB335':0.399, 'HRB400':0.384, 'RRB400':0.384}
eb_data = {'HPB300':0.576, 'HRB335':0.550, 'HRB400':0.518, 'RRB400':0.518}
p_min_data = {'HPB300':0.0025,'HRB335':0.0020,"HRB400":0.0020,"RRB400":0.0020}

df = pd.read_excel('data.xlsx')
rb_cat = df.loc[0, '数值']
fy = rb_data[rb_cat]
fc  = fc_data[df.loc[1, '数值']]
N = df.loc[2, '数值']*1000
M = df.loc[3, '数值']*1000000
b = df.loc[4, '数值']
h = df.loc[5, '数值']
a = df.loc[6, '数值']
a1 = df.loc[7, '数值']
lo = df.loc[8, '数值']
o = df.loc[9,'数值']
m = lo/h #计算判断是否要考虑纵向弯曲
if m <= 8:
    n = 1
    print('lo/h<=8,不考虑纵向弯曲,偏心距扩大系数为1')
else:
    print("lo/h>=8,需考虑纵向弯曲,要计算偏心距扩大系数")
    eo = M/N
    ho = h-a
    if eo <= (ho/30) :
        eo = ho/30
        print('eo<=ho/30,eo取值为%.3fmm'%eo)
    else:
        print('eo>ho/30,eo取原值,为:%.3fmm'%eo)
    w1 = (0.5*fc*b*h)/(1.2*N)
    if w1>1 :
        w1 = 1
        print('第一个影响系数为：%.3f'%w1)
    else:
        print('第一个影响系数为：%.3f'%w1)
    w2 = 1.15 - 10*m
    if (lo/h)<=15 :
        w2 = 1
        print('第二个影响系数为：%.3f'%w2)
    else:
        print('第一个影响系数为：%.3f'%w1)
    n = 1+((m)**2*w1*w2)/(1400*eo/ho)
    print('截面偏心距系数为：%.3f'%n)
m_1 = n*eo
eb = eb_data[rb_cat]
e = n*eo+h/2-a
print('压力N到远端的距离e:%.3fmm'%e)
asb = asb_data[rb_cat]

if m_1 >= 0.3*ho:

    print('n*eo>=0.3*ho,初步判断为大偏心,按大偏心步骤初步计算.')
    jgg = big_hr(fy,fc,N,b,h,a,a1,e,eo,eb,asb,rb_cat,n)
    e1 = jgg[0]
    jg = 1
    As = jgg[1]
    As1 = jgg[2]

else:
    print('n*eo<0.3*ho,初步判断为小偏心,按小偏心步骤初步计算.')
    jgg = small_hr(rb_cat,b,h,a,a1,eb,e,fc)
    As = jgg[1]
    As1 = jgg[2]
    jg = 0
if  jg==1:
    if e1 <= eb:
        print('计算,相对受压区高度---%.3f < 界限受压区高度---%.3f，大偏心计算是正确的，确为大偏心.'%(e1,eb))
    else:
        print('该题应为小偏心,以上计算的As和As1错误。按小偏心重新计算')
        jgg = small_hr(rb_cat,b,h,a,a1,eb,e,fc)
        As = jgg[1]
        As1 = jgg[2]
else:
    pass
jjgg = input('判断是否需要复核垂直于弯矩作用平面的承载力,是输入Y,否输入N。输入后按回车键.')
if jjgg == 'Y':
    print('复核垂直于弯矩作用平面的承载力')
    m_1 = lo/b
    print('lo/h=%.3f,请查表5-1得到稳定系数'%m_1)
    stdd = float(input('请输入稳定系数的值(直接输入数值):'))
    Nu = o*(fc*b*h+fy*(As+As1))
    print('N:%.3f'%N,'Nu:%.3f'%Nu)
    if N < (Nu/1.2):
        print('N=%.3f ,Nu/1.2 = %.3f ,N<(Nu/1.2),满足要求.'%(N,Nu))
    else:
        print('N=%.3f ,Nu/1.2 = %.3f ,N>(Nu/1.2),不满足要求.'%(N,Nu))
else:
    pass   
input()